package test5.lupeng.jf.com.teamdemo.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.activitys.BlogDetailsActivity;
import test5.lupeng.jf.com.teamdemo.bean.response.BlogResponse;

/**
 * Created by C-yx on 2017/7/27.
 */

public class BlogAdapter extends RecyclerView.Adapter {
    private static final int TYPE_HEAD = 0;//头布局
    private static final int TYPE_NORMAL = 1;//普通布局
    private View headView;
    ArrayList<BlogResponse.ResultBean> mHotBlogResultBeen;

    public BlogAdapter(ArrayList<BlogResponse.ResultBean> mHotBlogResultBeen) {
        this.mHotBlogResultBeen = mHotBlogResultBeen;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEAD) {
            return new HeadViewHolder(headView);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_study, parent, false);
        ItemViewHolder viewHolder = new ItemViewHolder(view);
        return viewHolder;
    }

    class HeadViewHolder extends RecyclerView.ViewHolder {

        public HeadViewHolder(View itemView) {
            super(itemView);
        }
    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        TextView tv_likeSize;
        TextView tv_time;
        TextView tv_author;
        ImageView iv_type;
        ImageView iv_headImage;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_likeSize = itemView.findViewById(R.id.tv_likeSize);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_author = itemView.findViewById(R.id.tv_author);
            iv_type = itemView.findViewById(R.id.iv_type);
            iv_headImage = itemView.findViewById(R.id.iv_headImage);
        }
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        int i = position;
        if (isHead(position)) {
            return;
        }
        if (headView != null) {
            position = position - 1;
        }

        final BlogResponse.ResultBean bean = mHotBlogResultBeen.get(position);
        ItemViewHolder viewHolder = (ItemViewHolder) holder;
        viewHolder.tv_title.setText(bean.getTitle());
        viewHolder.tv_likeSize.setText(bean.getLikeSize() + "");
        viewHolder.tv_author.setText(bean.getAuthor().getUsername());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(bean.getTimestamp()))));
        viewHolder.tv_time.setText(sd);
        Glide.with(holder.itemView.getContext()).load(bean.getAuthor().getHeadProtrait()).asBitmap()
                .into(viewHolder.iv_headImage);
        switch (bean.getBlogType()) {
            case 1:
                viewHolder.iv_type.setImageResource(R.drawable.ic_reader);
                break;
            case 2:
                viewHolder.iv_type.setImageResource(R.drawable.ic_help);

                break;
            case 3:
                viewHolder.iv_type.setImageResource(R.drawable.ic_movie);

                break;

        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(holder.itemView.getContext(), BlogDetailsActivity.class);
                intent.putExtra("blog", bean);
                holder.itemView.getContext().startActivity(intent);
            }
        });
        viewHolder.iv_headImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(holder.itemView.getContext(), "点击头像", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mHotBlogResultBeen.size() + (headView != null ? 1 : 0);
    }

    @Override
    public int getItemViewType(int position) {
        return isHead(position) ? TYPE_HEAD : TYPE_NORMAL;
    }

    private boolean isHead(int position) {
        return position == 0 && headView != null ? true : false;
    }

    public void setHeadView(View headView) {
        this.headView = headView;
    }
}
